<template>
  <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
      <el-form-item label="订单编号" prop="orderCode">
          <el-input
            v-model="queryParams.orderCode"
            placeholder="请输入订单编号"
            clearable
            class="!w-240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="箱码" prop="cartonCode">
          <el-input
            v-model="queryParams.cartonCode"
            placeholder="请输入箱码"
            clearable
            class="!w-240px"
             ref="nextFocus"
            @keyup.enter="handlebind(queryParams.cartonCode)"
          />
        </el-form-item>
        <!-- <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item> -->
      </el-form>
    </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
      <el-table v-loading="loading" :data="list">
        <!-- <el-table-column label="订单编号" align="center" prop="orderCode" />
        <el-table-column label="型号" align="center" prop="type" /> -->
        <el-table-column label="织板编码" align="center" prop="code" />
        <!-- <el-table-column label="织板规格" align="center" prop="specification" /> -->
        <el-table-column label="箱码" align="center" prop="cartonCode" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <!-- 用户导入对话框 -->
    <!-- <orderImportForm ref="importFormRef" @success="getList" /> -->
  </template>
  
  <script lang="ts" setup>
  import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  import download from '@/utils/download'
  import * as bindBoxCodeApi from '@/api/hcxly/bindBoxCode/Index'
  
  defineOptions({ name: 'bin' })
  
  const message = useMessage() // 消息弹窗
  const { t } = useI18n() // 国际化
  
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list = ref([]) // 列表的数据

  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    orderCode: '',
    cartonCode: ''
  })
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中
  
  /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await bindBoxCodeApi.getbindBoxCode(queryParams)
      console.log(data)
      list.value = data.list
      total.value = data.total
      console.log(list)
    } finally {
      loading.value = false
    }
  }

  // 获取dom
  const nextFocus = ref(null);
  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
    
    /** 回车聚焦到下一个输入框 */
    nextFocus.value.focus();
  }
  
  // /** 重置按钮操作 */
  // const resetQuery = () => {
  //   queryFormRef.value.resetFields()
  //   handleQuery()
  // }
  const formData = {
    orderCode: '',
    type: '',
    code: '',
    specification: '',
    cartonCode: ''
  }
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

  //比对箱码和织板编码
  const handlebind = async (cartonCodesPra: string) => {
    if(cartonCodesPra.length <= 0){
      return cartonCodesPra.length
    }
    const cartonMaterialCode = cartonCodesPra.split('_')
    const listArry = list.value
    const bindData = formData
    const result = ref()

    for(let i in listArry){
      
      if(cartonMaterialCode[0] === listArry[i].code){
        
        console.log(cartonMaterialCode[0]+'---'+listArry[i].code)
        console.log(listArry[i])
        bindData.orderCode = listArry[i].orderCode
        bindData.type = listArry[i].type
        bindData.code = listArry[i].code
        bindData.specification = listArry[i].specification
        bindData.cartonCode = cartonCodesPra
        loading.value = true
        try {
          console.log(bindData)
          result.value = await bindBoxCodeApi.bindBoxCodeEdit(bindData)
          console.log(result.value)
          message.success('绑定成功')
          queryParams.cartonCode = ''
          // 发送操作成功的事件
          emit('success')
          getList()
        // } catch{
        //   message.error('绑定失败，箱码与织板编码不匹配！')
        } finally {
          loading.value = false
        }
      }else{
        // message.error('绑定失败，箱码与织板编码不匹配！')
        // return
      }
    } 
  }
  
  /** 初始化 **/
  onMounted(() => {
    getList()
  })
  
  </script>
  